require('jade');
var express = require('express');
var auth = require('./auth');
auth = new auth();


module.exports = function(ip, port, sessionStore){
	var db = COLAMIX.DB;
	var app = express.createServer(ip);
	app.configure(function(){
		app.set('views', __dirname + '/../views');
		app.set('view engine', 'jade');
		app.set('view options', {layout: false});
		
		app.use(express.logger('dev'));
		app.use(express.cookieParser());
		app.use(express.session({ 
			store: sessionStore,
			secret: "secretString"
		}));
		app.use(express.bodyParser());
		app.use(app.router);
		app.use(express.static(__dirname + '/../public'));
		app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
	});
	
	
	/**
	 *  Controller section
	 */
	app.get('/', auth.restrictTo('member'), function(req, res){
	    res.render('layout');
	});
	app.get('/denied', function(req, res){
	    res.render('denied');
	});
	app.get('/login', function(req, res){
		//if logedin redirect
	    res.render('login');
	});
	app.post('/login', auth.login);
	app.get('/logout', auth.logout);


	app.get('/player', auth.restrictTo('member'), function(req, res){
	    res.render('player');
	});
	/**
	 * controller end
	 */
	
	
	
	
	app.listen(port);
	return {
		app: app,
	};
}